package com.jzr.pda.db1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.pda.common.SecurityHelper;
import com.jzr.pda.db1.mapper.TVitalSignMapper;
import com.jzr.pda.db1.model.TVitalSign;
import com.jzr.pda.db1.service.TVitalSignService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class TVitalSignServiceImpl extends ServiceImpl<TVitalSignMapper,TVitalSign> implements TVitalSignService {
    @Override
    public Integer save2(TVitalSign record) {
        QueryWrapper<TVitalSign> qw = new QueryWrapper<>();
        qw.eq(TVitalSign.PatientCureNo_,record.getPatientCureNo())
                .eq(TVitalSign.OpDate_,record.getOpDate())
                .eq(TVitalSign.OpTime_,record.getOpTime())
                .isNull(TVitalSign.IsDel_);
        List<Integer> list = baseMapper.getIdList(qw);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        record.setVitalNo(record.getPatientCode()+record.getPatientCureNo()+record.getOpDate().format(formatter)+record.getOpTime());
        record.setUpdateBy(SecurityHelper.getUserId());
        record.setUpdateTime(LocalDateTime.now());
        record.setIsDel(null);
        if(list != null && list.size() > 0){
            record.setId(list.get(0));
            return baseMapper.updateById(record);
        }else {
            return baseMapper.insert(record);
        }
    }
}
